<?php
namespace app\mobile\controller;
use think\Controller;
use think\Controllers;
use think\Session;
use think\Cookie;
use think\Jump;
use think\Db;
class Ajax extends Controllers{

	public function getorder(){
		$where=' uniacid=2';
		
		if($_REQUEST['name']){
			$where.=' and fb='{$_REQUEST['name']}' ';
		}
		
	    $list=DB::query("select fee,fb,time,sb from ims_xuan_zhifu_order where ".$where." order by id desc limit 10");

	    echo json_encode($list);die;

	}
	public function info(){
		$member = Session::get("user");
	    if($member['uid']){
        $me=DB::query("SELECT * FROM ims_xuan_zy_person where uid='{$member['uid']}' ")[0];
        echo json_encode(array(
            'ret'=>'0',
            'app'=>array('aiPhone'=>$me['mobile'],'aiRealname'=>$me['name'],'aiIdCard'=>$me['idcard'])
        ));die;
    }else{
        echo json_encode(array(
            'ret'=>'-1'
        ));die;
  	  } 
	}
	public function zhuce(){

	}
	public function sms (){
		    //发送短信验证码
    $mobile=$_REQUEST['phone'];
    $type=$_REQUEST['type'];
    $code=rand(1000,9999);
    //$code=1234;
    if($_COOKIE["code".$type]){
        echo json_encode(array('ret'=>-1));die;
    }else{
        $_SESSION["code".$type]=$code;
        $_SESSION["phone".$type]=$mobile;

        setcookie("code".$type, 1234, time()+60);
        setcookie("phone", 1234, time()+60);
    }
    

    /*$content='您的验证码是'.$code;
    //$re=$this->baoSendSMS($mobile,$content,$config);

    include '../addons/xuan_zy/lib/alisms/sendSms.php';
    $re=sendSms($config,$mobile,$code);
    $re=json_decode(json_encode($re),1);
    if($re['Code']=='OK'){
        echo json_encode(array('ret'=>0,'msg'=>'发送成功'));
    }else{
        echo json_encode(array('ret'=>-1,'msg'=>'发送失败，错误代码'.$re['Message']));
    }*/
    $content="【卡易淘】您的验证码{$code}，该验证码10分钟内有效，请勿泄露于他人！";
    $re=chuangLanSendSMS($mobile,$content);
    echo json_encode(array('ret'=>0,'msg'=>'发送成功'));
	}
	  public function chuangLanSendSMS($mobile, $msg, $rt='json' ){
        //云信接口参数
        $postArr = array (
            'action' =>'send',//短信发送发送
            'account'  =>  '922197',
            'password' => 'Z7eHxZ',
            'mobile' => $mobile,
            'content' => $msg,
            'extno'  => '10690197',
            'rt' => $rt
        );
        $result = curlPost( 'http://47.100.188.160:7862/sms' , $postArr);
        return $result;

    }

    /**
     * 通过CURL发送HTTP请求
     * @param string $url  //请求URL
     * @param array $postFields //请求参数 
     * @return mixed
     */
    private function curlPost($url,$postFields){
        
        $postFields = http_build_query($postFields);
        
        $ch = curl_init ();
        curl_setopt( $ch, CURLOPT_URL, $url );      
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt( $ch, CURLOPT_TIMEOUT,60); 
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec ( $ch );

        if (false == $ret) {
            $result = curl_error(  $ch);
        } else {
            $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                $result = "请求状态 ". $rsp . " " . curl_error($ch);
            } else {
                $result = $ret;
            }
        }
        curl_close ( $ch );        
        return $result;
    }
    public function mobilecheck(){
		$member = Session::get("user");
	    $mobile=$_REQUEST['phoneNum'];
	    $me=DB::query("SELECT * FROM ims_xuan_zy_person where mobile='{$mobile}' and uid!='{$member['uid']}' ")[0];
	    $me2=DB::query("SELECT * FROM ims_c_members where mobile='{$mobile}' and uid!='{$member['uid']}' ")[0];

	    if($me || $me2){
	        echo json_encode(array('ret'=>2));die;
	    }else{
	        echo json_encode(array('ret'=>1));die;
	    }
    }
    public function zxorder(){
    	 $name=$_REQUEST['name'];
    $idcard=$_REQUEST['idcard'];
    $mobile=$_REQUEST['mobile'];
    $id=$_REQUEST['id'];
    $re2=pdo_fetch(' select * from  '.tablename('xuan_zy_zxsetting').' where id=:id ',array(':id'=>$id));
    if(!empty($name) && !empty($idcard) && !empty($mobile) && $re2){

        $orderid='ZX-'.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $user_data = array(
            'uid' => $member['uid'],
            'pay'=>0,
            'money'=>$re2['money'],
            'out_trade_no' => $orderid,
            'zxid' => $id,
            'tuikuan'=> 0,
            'name'=>$name,
            'status'=>1,
            'mobile'=>$mobile,
            'idcard'=>$idcard,
            'createtime' => time(),
            'uniacid' => $_W['uniacid'],
        );
        $result = DB::table("ims_xuan_zy_zxorder")->insert($user_data);
        if($result){
            echo json_encode(array('status'=>1,'tid'=>$orderid));die;
        }

    }
   		 echo json_encode(array('status'=>0));die;
    }
    public function getarticle(){
	    $pindex    = max(1, intval($_REQUEST['page']));

	    $psize     = 20;

	    $wheres='  uniacid=2';


	    if(!empty($_REQUEST['keywords'])){

	        $wheres.=" and (title LIKE '%".$_REQUEST['keywords']."%' or jianjie LIKE '%".$_REQUEST['keywords']."%'  ) ";
	        $_REQUEST['cate_id']=0;

	    }

	    if($_REQUEST['cate_id']==0){
	        $wheres.=' and lanmu>0 ';
	    }else{
	        if($_REQUEST['cate_id']==-6){
	            $wheres.=" and uid= '{$member['uid']}' ";
	        }
	        $wheres.=" and lanmu='{$_REQUEST['cate_id']}' ";
	    }




	    $sql = "SELECT * FROM ims_xuan_zy_article WHERE   {$wheres}  order by id desc";

	    $sql .= " limit " . ($pindex - 1) * $psize . ',' . $psize;

	    $list = DB::query($sql);



	    $total = DB::query("select count(*) as count from ims_xuan_zy_article  WHERE  {$wheres} ")[0]['count'];
	    header('Content-Type:application/json');
	    echo json_encode($list);die;
    }
    public function getarticle(){

		$member = Session::get("user");
	    $pindex    = max(1, intval($_REQUEST['page']));

	    $psize     = 20;

	    $wheres='  uniacid=2 ';

	    if(!empty($_REQUEST['keywords'])){

	        $wheres.=" and (title LIKE '%".$_REQUEST['keywords']."%' or jianjie LIKE '%".$_REQUEST['keywords']."%'  ) ";
	        $_REQUEST['cate_id']=0;

	    }

	    if($_REQUEST['cate_id']==0){
	        $wheres.=' and lanmu > 0 ';
	    }else{
	        if($_REQUEST['cate_id']==-6){
	            $wheres.=" and uid={$member['uid']} ";
	        }
	        $wheres.=" and lanmu='{$_REQUEST['cate_id']}' ";
	    }




	    $sql = "SELECT * FROM ims_xuan_zy_article WHERE   {$wheres}  order by id desc";

	    $sql .= " limit " . ($pindex - 1) * $psize . ',' . $psize;

	    $list = DB::query($sql);



	    $total = DB::query("select count(*) as count from ims_xuan_zy_article WHERE  {$wheres} ")[0]['count'];
	    header('Content-Type:application/json');
	    echo json_encode($list);die;
    }
    public function setorder(){
        $bank_id  = $_REQUEST['id'];
        $member = Session::get("user");
        $sql = "select * from ims_xuan_zy_bankorder where bank_id = '{$bank_id}' and uid = '{$member['uid']}'";
        $bank = DB::query($sql)[0];
        if(!$bank){

            $item = DB::query("select * from ims_xuan_zy_bank where id = {$bank_id}")[0];
            $upsuer = DB::query("select * from ims_xuan_zy_userup where uid = '{$member['uid']}'")[0];
            $person = DB::query("select * from ims_xuan_zy_person where uid = '{$member['uid']}'")[0];
            $data=array(
                'user_id'=>$member['uid'],
                'bank_id'=>$item['id'],
                'suid'=>$upsuer['suid'],
                'ssuid'=>$upsuer['ssuid'],
                'sssuid'=>$upsuer['sssuid'],

                'mobile'=>$person['mobile'],
                'bank_name'=>$item['bankname'],
                'hide_name'=>$person['name'],

                'idcard'=>$person['idcard'],
                'status'=>1,
                'create_time'=>date('Y-m-d H:i:s',time()),
                'uniacid'=>2
            );
            DB::table("ims_xuan_zy_bankorder")->insert($data);
        }

    }
}